package tgc.edu.exam.controller;

import java.util.HashSet;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.custom.DataGridParam;
import tgc.edu.exam.custom.DataGridUtils;
import tgc.edu.exam.entity.Permission;
import tgc.edu.exam.form.PermissionForm;
import tgc.edu.exam.service.PermissionService;


/**
 * 权限管理controller
 * @author zz
 *
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {
	@Autowired
	private PermissionService permissionService;
	
	@RequestMapping("/manage")
	public String manage() {
		return "permission/manage";
	}
	
	@RequestMapping("/page")
	@ResponseBody
	public Object page(DataGridParam param, PermissionForm form) {
		Pageable pageable = param.getPageable();
		Specification<Permission> spec = buildSpec(form);
		Page<Permission> page = permissionService.findAll(spec,pageable);
		return DataGridUtils.buildResult(page);
	}

	private Specification<Permission> buildSpec(PermissionForm form) {
		
		Specification<Permission> spec = new Specification<Permission>() {
			private static final long serialVersionUID = 1L;
			@Override
			public Predicate toPredicate(Root<Permission> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				String name = form.getName();
				String code = form.getCode();
				 if (StringUtils.hasText(code)) {
						rules.add(cb.like(root.get("code"), "%"+code+"%"));
					}
				 else if (StringUtils.hasText(name)) {
					rules.add(cb.like(root.get("name"), "%"+name+"%"));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	
	@RequestMapping("/edit")
	public String edit(PermissionForm form,ModelMap map) {
		Permission roles = new Permission();
		if(form.getId()!=null) {
			roles = permissionService.findById(form.getId());
		}	
		map.put("model", roles);
		return "permission/edit";
	}

	@RequestMapping("/save")
	@ResponseBody
	public Object save(PermissionForm form) {
		if(form.getId()==null) {
			Permission per = new Permission();
			per.setName(form.getName());
			per.setCode(form.getCode());
			permissionService.save(per);
			return "新增成功!";
		}
		else {
			Permission model = permissionService.findById(form.getId());
			model.setName(form.getName());
			model.setCode(form.getCode());
			permissionService.save(model);
			return "保存成功!";
		}
	}
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Integer id) {
		permissionService.deleteById(id);
		return "删除成功!";
	}
}
